<?php
# 文件名称:config.php
class config extends C_controller {
	function config()
	{
		parent::C_controller();
		//设置语言
		$this->setLanguageFile( 'config' );
		//任务列表
		$this->registerTask( 'add'  , 	'display'  );
		$this->registerTask( 'edit'  , 	'display'  );
		$this->registerTask( 'apply', 	'save'  );
	}
	
	function display()
	{
		//判断是否有权限
		$this->permit('config.display');
		parent::display($this->getTask());
	}
	function save()
	{
		//检查表单是否合法有效
		C_request::checkToken();
		$this->permit('config.save');
		$SYS = &get_instance();
		$db = C_loader::getDBO();
		//获取提交数据
		$data = $SYS->request->get('post',4);
		
		//写入多语言字段
		$fieldstable = C_loader::getDTO('fields');
		//获取开启的前台语言,默认语言为首
		$tmplang = $SYS->lang->getALL(1,'backend');
		//设置网站标题
		for($i=0;$i<count($tmplang);$i++)
		{
			$tmpfields = array();
			$tmpid = $tmplang[$i]->id;
			$tmpfields['lang'] = $tmpid;
			//判断多语言字段
			$query = 'SELECT * '.
					' FROM '.$fieldstable->_db->_table_prefix.'fields'.
					' WHERE recordid=0'.
					' AND module=0'.
					' AND fieldname="sitename"'.
					' AND lang='.$tmpid
			;
			$fieldstable->_db->setQuery($query);
			if($fieldstable->_db->loadResult() != null)
			{
				$fieldstable->load($data['sitenameid'][$tmpid]);
				$tmpfields['fieldvalue'] = $data['sitename'][$tmpid];
			}else
			{
				$tmpfields['recordid'] = 0;
				$tmpfields['module'] = 0;
				$tmpfields['fieldname'] = "sitename";
				$tmpfields['fieldvalue'] = $data['sitename'][$tmpid];
			}			
			// Bind the data.
			if (!$fieldstable->bind($tmpfields)) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			// 保存
			if (!$fieldstable->store()) {
				$SYS->loader->setMessage(C_text::sprintf( 'Tip5'),'error');
				$this->display();
				return;
			}
			//防止错误更新
			$fieldstable->id=0;
			unset($tmpfields);
		}
		//保存配置文件
		$toCfg = &$SYS->config->arrayToString(array_merge($SYS->config->config,$data['config']));
		$SYS->loader->helper('file');
		$result = write_file(PATH_CONFIGURATION.DS.'config.php',$toCfg);
		if($result) {$SYS->loader->setMessage(C_text::_('Tip1'));$SYS->config->reload('config');}
		else	$SYS->loader->setMessage(C_text::_('Tip6'),'error');
		//处理缓存
		if($SYS->config->item('caching') == 0)
		{
			$cache = _import('cache/cache');
			$cache->clean(true);
		}
		$this->display();
	}
}
?>